(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dashboardBuyer.panelsController', panelsController);
	panelsController.$inject = [ '$scope', '$q', 'Constants', 'layoutConfig', 'authService', 'validatorConfig', 'ChartJs', 'dashboardService' ];
	function panelsController($scope, $q, Constants, layoutConfig, authService, validatorConfig, ChartJs, dashboardService) {
		var ctrl = this;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.$onInit = function() {
			dashboardService.getPageConfig('DASHBOARD_BUYER').then(function(response) {
				ctrl.pageConfig = response.pageConfig;
				dashboardService.getPagePanelConfigs(response.dbdPageId).then(function(response) {
					if(response.panelConfigs){
						sort(ctrl.pageConfig, response.panelConfigs);
						
						angular.forEach(response.panelConfigs, function(panelConfig) {
							panelConfig.controllerJsPrefix = "apf.dashboard.";
						});
						
						ctrl.pageConfig.panelConfigs = response.panelConfigs;
					}
				});
			});
		};

		function sort(pageConfig, panelConfigs) {
			if (pageConfig.layoutType == 'O') {
				panelConfigs.sort(function(a, b) {
					return a.panelOrder - b.panelOrder;
				});
			}
		}
	}
})(angular);